<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
<title>首页 - 华鑫农业后台管理系统后台管理系统</title>
<link rel="icon" href="favicon.ico" type="image/ico">
<link href="../css/bootstrap.min.css" rel="stylesheet">
<link href="../css/materialdesignicons.min.css" rel="stylesheet">
<link href="../css/style.min.css" rel="stylesheet">
  <script type="text/javascript" src="../js/jquery.min.js"></script>
  <script src="../js/myScript/app.js"></script>
  <script type="text/javascript" src="../js/bootstrap.min.js"></script>
  <script type="text/javascript" src="../js/perfect-scrollbar.min.js"></script>
  <script type="text/javascript" src="../js/main.min.js"></script>
<%--  <script type="text/javascript" src="../js/template-web.js"></script>--%>
</head>

<body>
<div class="lyear-layout-web">
  <div class="lyear-layout-container">
    <%--    菜单栏--%>
    <jsp:include page="left_nav.jsp"/>
      <script type="text/javascript">

        for (let i = 1; i <= 6; i++) {
          $("#nav" + i).attr("class", "nav-item");
        }
        $("#nav2").attr("class", "nav-item active");
      </script>
    <%--头部--%>
    <jsp:include page="header.jsp"/>


      <!--页面主要内容-->
      <main class="lyear-layout-content">

        <div class="container-fluid">

          <div class="row">
            <div class="col-lg-12">
              <div class="card">
                <div class="card-toolbar clearfix">

                  <%--                搜索功能的页面--%>
                  <form class="pull-right search-bar" method="get" action="#!" role="form">
                    <div class="input-group">
                      <div class="input-group-btn">
                        <input type="hidden" name="search_field" id="search-field" value="title">
                        <button class="btn btn-default dropdown-toggle" id="search-btn" data-toggle="dropdown" type="button" aria-haspopup="true" aria-expanded="false">
                          内容 <span class="caret"></span>
                        </button>
                        <ul class="dropdown-menu">
                          <li> <a tabindex="-1" href="javascript:void(0)" data-field="title">内容</a> </li>
                          <li> <a tabindex="-1" href="javascript:void(0)" data-field="cat_name">时间</a> </li>
                        </ul>
                      </div>
                      <input type="text" class="form-control" value="" name="keyword" placeholder="请输入名称">
                    </div>
                  </form>
                  <div class="toolbar-btn-action">
                    <a class="btn btn-primary m-r-5" href="#!" data-toggle="modal" data-target="#myModal"><i class="mdi mdi-plus" ></i> 新增</a>
                  </div>
                </div>
                <div class="card-body">

                  <div class="table-responsive">
                    <table class="table table-bordered">
                      <thead>
                      <tr>

                        <th>编号</th>
                        <th>联系地点</th>
                        <th>公司联系热线</th>
                        <th>邮箱</th>
                        <th>详细地址</th>
                        <th>操作</th>
                      </tr>
                      </thead>
                      <tbody id="tb_data">
                      </tbody>
                    </table>
                  </div>


                  <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
                    <div class="modal-dialog" role="document">
                      <div class="modal-content">
                        <div class="modal-header">
                          <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                          <h4 class="modal-title" id="myModalLabel">新增公司联系方式</h4>
                        </div>
                        <div class="modal-body">
                          <div class="row">
                            <div class="col-lg-12">
                              <div class="card">
                                <div class="card-body">

                                  <form action="#!" method="post" class="row">

                                    <div class="form-group col-md-12">
                                      <label for="contactAdd">联系地点</label>
                                      <input type="text" class="form-control" id="contactAdd" name="title" value="" placeholder="联系地点" />
                                    </div>

                                    <div class="form-group col-md-12">
                                      <label for="hotCall">公司联系热线</label>
                                      <input type="text" class="form-control" id="hotCall" name="seo_keywords" value="" placeholder="公司联系热线" />
                                    </div>

                                    <div class="form-group col-md-12">
                                      <label for="email">邮箱</label>
                                      <input type="text" class="form-control" id="email" name="seo_keywords" value="" placeholder="xxxxxx@qq.com" />
                                    </div>

                                    <div class="form-group col-md-12">
                                      <label for="addIndroduce">详细地址</label>
                                      <textarea class="form-control" id="addIndroduce" name="seo_description" rows="5" value="" placeholder="详细地址"></textarea>
                                    </div>
                                  </form>
                                </div>
                              </div>
                            </div>
                          </div>
                        </div>
                        <div class="modal-footer">
                          <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                          <button type="button" class="btn btn-primary" data-dismiss="modal" onclick="addCompanyItem()">点击新增</button>
                        </div>
                      </div>
                    </div>
                  </div>

                  <div class="modal fade" id="myModal2" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
                    <div class="modal-dialog" role="document">
                      <div class="modal-content">
                        <div class="modal-header">
                          <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                          <h4 class="modal-title" id="myModalLabel2">更新公司联系方式</h4>
                        </div>
                        <div class="modal-body">
                          <div class="row">
                            <div class="col-lg-12">
                              <div class="card">
                                <div class="card-body">

                                  <form action="#!" method="post" class="row">

                                    <div class="form-group col-md-12">
                                      <label for="contactAdd2">联系地点</label>
                                      <input type="text" class="form-control" id="contactAdd2" name="title" value="" placeholder="联系地点" />
                                    </div>

                                    <div class="form-group col-md-12">
                                      <label for="hotCall2">公司联系热线</label>
                                      <input type="text" class="form-control" id="hotCall2" name="seo_keywords" value="" placeholder="公司联系热线" />
                                    </div>

                                    <div class="form-group col-md-12">
                                      <label for="email2">邮箱</label>
                                      <input type="text" class="form-control" id="email2" name="seo_keywords" value="" placeholder="xxxxxx@qq.com" />
                                    </div>

                                    <div class="form-group col-md-12">
                                      <label for="addIndroduce2">详细地址</label>
                                      <textarea class="form-control" id="addIndroduce2" name="seo_description" rows="5" value="" placeholder="详细地址"></textarea>
                                    </div>
                                  </form>
                                </div>
                              </div>
                            </div>
                          </div>                          </div>
                        <div class="modal-footer">
                          <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                          <button type="button" class="btn btn-primary" data-dismiss="modal" onclick="onClickUpdata()">点击保存</button>
                        </div>
                      </div>
                    </div>
                  </div>
                  <%--                分页，先不实现--%>
                  <%--                <ul class="pagination">--%>
                  <%--                  <li class="disabled"><span>«</span></li>--%>
                  <%--                  <li class="active"><span>1</span></li>--%>
                  <%--                  <li><a href="#1">2</a></li>--%>
                  <%--                  <li><a href="#1">3</a></li>--%>
                  <%--                  <li><a href="#1">4</a></li>--%>
                  <%--                  <li><a href="#1">5</a></li>--%>
                  <%--                  <li><a href="#1">6</a></li>--%>
                  <%--                  <li><a href="#1">7</a></li>--%>
                  <%--                  <li><a href="#1">8</a></li>--%>
                  <%--                  <li class="disabled"><span>...</span></li>--%>
                  <%--                  <li><a href="#!">14452</a></li>--%>
                  <%--                  <li><a href="#!">14453</a></li>--%>
                  <%--                  <li><a href="#!">»</a></li>--%>
                  <%--                </ul>--%>

                </div>
              </div>
            </div>

          </div>

        </div>

      </main>
      <!--End 页面主要内容-->
  </div>
</div>


<script type="text/javascript">
  var resData;
  function qeuryAllContact() {
    $.ajax({
      type: "get",
      async: true,
      url: serverUrl + "/contact/queryContact",
      xhrFields: {
        withCredentials: true
      },
      dataType: "json",
      success: function (res) {
        if (res.success) {
          //请求成功
          resData = res.data;
          for (let i = 0; i < resData.length; i++) {
            var item = resData[i];

            $("#tb_data").append("<tr>\n" +
                    "                      <td id=\""+ item.ctId +"\">"+ item.ctId +"</td>\n" +
                    "                      <td>"+ item.ctAdd +"</td>\n" +
                    "                      <td>" + item.ctCall + "</td>\n" +
                    "                      <td>" + item.ctEmail + "</td>\n" +
                    "                      <td>" + item.ctLocation + "</td>\n" +
                    "                      <td>\n" +
                    "                        <div class=\"btn-group\">\n" +
                    "                          <a id=\'"+ item.ctId +"\' class=\"btn btn-xs btn-default\" href=\"#!\" title=\"编辑\"  data-toggle=\"modal\" data-target=\"#myModal2\" onclick='upDataContactItem(this)'><i class=\"btn btn-sm btn-primary\">编辑</i></a>\n" +
                    "                          <a id=\'"+ item.ctId +"\' class=\"btn btn-xs btn-default\" href=\"#!\" title=\"删除\" data-toggle=\"tooltip\" onclick='deleteContactItem(this)'><i class=\"btn btn-sm btn-danger\">删除</i></a>\n" +
                    "                        </div>\n" +
                    "                      </td>\n" +
                    "                    </tr>");

          }
        } else {
          alert(res.msg);
        }

      },
      error: function (res) {
        alert("请求出错");
      }
    });
  }

  //加载页面调用一次
  qeuryAllContact();
  /**
   * 新增功能
   * @param cyId
   */
  function addCompanyItem() {

    //获取到text文本框内容
    var hotCall =  $("#hotCall").val();
    var contactAdd =  $("#contactAdd").val();
    var email =  $("#email").val();
    var addIndroduce =  $("#addIndroduce").val();
    //发送ajax请求
    $.ajax({
      type: "POST",
      contentType:"application/json;charset=UTF-8",
      url: serverUrl + "/contact/addContact",
      xhrFields: {
        withCredentials: true
      },
      data: JSON.stringify({"ctAdd":contactAdd, "ctCall": hotCall, "ctEmail": email, "ctLocation": addIndroduce}),
      dataType: "json",
      success: function (res) {
        if (res.success) {
          //清楚表，数据然后，调用ajax请求查询一次新数据
          $("#tb_data").empty();
          qeuryAllContact();
        } else {
          alert(res.msg);
        }
      },
      error: function (res) {
        alert("请求出错");
      }
    });

    $("#hotCall").val("");
    $("#contactAdd").val("");
    $("#email").val("");
    $("#addIndroduce").val("");

  }

  /**
   * 修改指定的简介
   */
  function upDataContactItem(element) {
    //修改的id号
    var cyId = element.getAttribute("id");
    var hotCall =  $("#hotCall2");
    var contactAdd =  $("#contactAdd2");
    var email =  $("#email2");
    var addIndroduce =  $("#addIndroduce2");

    hotCall.attr("name", cyId);
    $.ajax({
      type: "GET",
      url: serverUrl + "/contact/queryContactById?CtId=" + cyId,
      xhrFields: {
        withCredentials: true
      },
      dataType: "json",
      success: function (res) {
        if (res.success) {
          hotCall.val(res.data.ctCall);
          contactAdd.val(res.data.ctAdd);
          email.val(res.data.ctEmail);
          addIndroduce.val(res.data.ctLocation);
        } else {
          alert(res.msg);
        }
      },
      error: function (res) {
        alert("请求失败");
      }
    });
  }

  /**
   * 保存更新的数据
   */
  function onClickUpdata() {
    var hotCall =  $("#hotCall2").val();
    var contactAdd =  $("#contactAdd2").val();
    var email =  $("#email2").val();
    var addIndroduce =  $("#addIndroduce2").val();

    var id = $("#hotCall2").attr("name");
    $.ajax({
      type: "POST",
      contentType: "application/json;charset=utf-8",
      url: serverUrl + "/contact/updateContact",
      xhrFields: {
        withCredentials: true
      },
      data: JSON.stringify({"ctId":id, "ctAdd": contactAdd, "ctCall": hotCall, "ctEmail": email, "ctLocation":addIndroduce}),
      dataType: "json",
      success: function (res) {
          if (res.success) {
            $("#tb_data").empty();
            qeuryAllContact();
          } else {
            alert("更新数据失败")
          }
      },
      error: function (res) {
        alert("请求错误");
      }
    })
  }
  /**
   * 删除选中的item
   */
  function deleteContactItem(element) {
    //修改的id号
    var cyId = element.getAttribute("id");
    $.ajax({
      type: "GET",
      url: serverUrl + "/contact/deleteContact?CtId=" + cyId,
      xhrFields: {
        withCredentials: true
      },
      dataType: "json",
      success: function (res) {
        if (res.success) {
          //清楚表，数据然后，调用ajax请求查询一次新数据
          $("#tb_data").empty();
          qeuryAllContact();
        }
      },
      error: function (res) {
        alert("请求出错");
      }
    });
  }
</script>
</body>
</html>
